using System;
using System.Windows;
using System.Windows.Media;
using NUnit.Framework;
using SCSFContrib.CompositeUI.WPF.Workspaces;

namespace SCSFContrib.CompositeUI.WPF.UnitTest.Workspaces
{
	[TestFixture]
	public sealed class WindowSmartPartInfoTest : UnitTest
	{
		private WindowSmartPartInfo _windowSmartPartInfo;

		protected override void SetUpCore()
		{
			base.SetUpCore();
			_windowSmartPartInfo = new WindowSmartPartInfo();
		}

		[Test]
		public void Constructor_ShouldAssignCorrectDefaults()
		{
			Assert.IsFalse(_windowSmartPartInfo.IsModal);
			Assert.IsNull(_windowSmartPartInfo.Width);
			Assert.IsNull(_windowSmartPartInfo.Height);
			Assert.IsNull(_windowSmartPartInfo.MinWidth);
			Assert.IsNull(_windowSmartPartInfo.MinHeight);
			Assert.IsNull(_windowSmartPartInfo.MaxWidth);
			Assert.IsNull(_windowSmartPartInfo.MaxHeight);
			Assert.IsNull(_windowSmartPartInfo.Left);
			Assert.IsNull(_windowSmartPartInfo.Top);
			Assert.IsNull(_windowSmartPartInfo.ShowInTaskbar);
			Assert.IsNull(_windowSmartPartInfo.IsTopmost);
			Assert.IsNull(_windowSmartPartInfo.Icon);
			Assert.IsNull(_windowSmartPartInfo.ResizeMode);
			Assert.IsNull(_windowSmartPartInfo.SizeToContent);
			Assert.AreEqual(WindowStartupLocation.Manual, _windowSmartPartInfo.StartupLocation);
			Assert.IsNull(_windowSmartPartInfo.State);
			Assert.IsNull(_windowSmartPartInfo.Style);
			Assert.IsNull(_windowSmartPartInfo.WpfStyle);
			Assert.IsNull(_windowSmartPartInfo.Owner);
			Assert.IsNull(_windowSmartPartInfo.DialogResult);
		}

		[Test]
		public void Constructor_Title_ShouldAssignTitle()
		{
			_windowSmartPartInfo = new WindowSmartPartInfo("Some title");
			Assert.AreSame("Some title", _windowSmartPartInfo.Title);
		}

		[Test]
		public void Constructor_TitleWindow_ShouldAssignWindowToOwner()
		{
			Window owner = new Window();
			_windowSmartPartInfo = new WindowSmartPartInfo("Some title", owner);
			Assert.AreSame("Some title", _windowSmartPartInfo.Title);
			Assert.AreSame(owner, _windowSmartPartInfo.Owner);
		}

		[Test]
		public void Properties_ShouldGetAndSetCorrectly()
		{
			ImageSource icon = new DrawingImage();
			Style style = new Style();
			Window owner = new Window();

			_windowSmartPartInfo.IsModal = true;
			_windowSmartPartInfo.Width = 200;
			_windowSmartPartInfo.Height = 300;
			_windowSmartPartInfo.MinWidth = 50;
			_windowSmartPartInfo.MinHeight = 40;
			_windowSmartPartInfo.MaxWidth = 400;
			_windowSmartPartInfo.MaxHeight = 350;
			_windowSmartPartInfo.Left = 45;
			_windowSmartPartInfo.Top = 55;
			_windowSmartPartInfo.ShowInTaskbar = false;
			_windowSmartPartInfo.IsTopmost = true;
			_windowSmartPartInfo.Icon = icon;
			_windowSmartPartInfo.ResizeMode = ResizeMode.CanResizeWithGrip;
			_windowSmartPartInfo.SizeToContent = SizeToContent.Height;
			_windowSmartPartInfo.StartupLocation = WindowStartupLocation.CenterOwner;
			_windowSmartPartInfo.State = WindowState.Maximized;
			_windowSmartPartInfo.Style = WindowStyle.None;
			_windowSmartPartInfo.WpfStyle = style;
			_windowSmartPartInfo.Owner = owner;

			Assert.IsTrue(_windowSmartPartInfo.IsModal);
			Assert.AreEqual(200, _windowSmartPartInfo.Width);
			Assert.AreEqual(300, _windowSmartPartInfo.Height);
			Assert.AreEqual(50D, _windowSmartPartInfo.MinWidth);
			Assert.AreEqual(40D, _windowSmartPartInfo.MinHeight);
			Assert.AreEqual(400D, _windowSmartPartInfo.MaxWidth);
			Assert.AreEqual(350D, _windowSmartPartInfo.MaxHeight);
			Assert.AreEqual(45D, _windowSmartPartInfo.Left);
			Assert.AreEqual(55D, _windowSmartPartInfo.Top);
			Assert.IsFalse(_windowSmartPartInfo.ShowInTaskbar.Value);
			Assert.IsTrue(_windowSmartPartInfo.IsTopmost.Value);
			Assert.AreSame(icon, _windowSmartPartInfo.Icon);
			Assert.AreEqual(ResizeMode.CanResizeWithGrip, _windowSmartPartInfo.ResizeMode);
			Assert.AreEqual(SizeToContent.Height, _windowSmartPartInfo.SizeToContent);
			Assert.AreEqual(WindowStartupLocation.CenterOwner, _windowSmartPartInfo.StartupLocation);
			Assert.AreEqual(WindowState.Maximized, _windowSmartPartInfo.State);
			Assert.AreEqual(WindowStyle.None, _windowSmartPartInfo.Style);
			Assert.AreSame(style, _windowSmartPartInfo.WpfStyle);
			Assert.AreSame(owner, _windowSmartPartInfo.Owner);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ApplyTo_Window_ShouldThrowIfWindowIsNull()
		{
			_windowSmartPartInfo.ApplyTo((Window) null);
		}

		[Test]
		public void ApplyTo_Window_ShouldApplyPropertiesToGivenWindow()
		{
			Style style = new Style();
			Window window = new Window();

			_windowSmartPartInfo.Title = "Window Title";
			_windowSmartPartInfo.IsModal = true;
			_windowSmartPartInfo.Width = 200;
			_windowSmartPartInfo.Height = 300;
			_windowSmartPartInfo.MinWidth = 50;
			_windowSmartPartInfo.MinHeight = 40;
			_windowSmartPartInfo.MaxWidth = 400;
			_windowSmartPartInfo.MaxHeight = 350;
			_windowSmartPartInfo.Left = 45;
			_windowSmartPartInfo.Top = 55;
			_windowSmartPartInfo.ShowInTaskbar = false;
			_windowSmartPartInfo.IsTopmost = true;
			_windowSmartPartInfo.ResizeMode = ResizeMode.CanResizeWithGrip;
			_windowSmartPartInfo.SizeToContent = SizeToContent.Height;
			_windowSmartPartInfo.StartupLocation = WindowStartupLocation.CenterOwner;
			_windowSmartPartInfo.State = WindowState.Maximized;
			_windowSmartPartInfo.Style = WindowStyle.None;
			_windowSmartPartInfo.WpfStyle = style;

			_windowSmartPartInfo.ApplyTo(window);

			Assert.AreEqual("Window Title", window.Title);
			Assert.AreEqual(200, window.Width);
			Assert.AreEqual(300, window.Height);
			Assert.AreEqual(50D, window.MinWidth);
			Assert.AreEqual(40D, window.MinHeight);
			Assert.AreEqual(400D, window.MaxWidth);
			Assert.AreEqual(350D, window.MaxHeight);
			Assert.AreEqual(45D, window.Left);
			Assert.AreEqual(55D, window.Top);
			Assert.IsFalse(window.ShowInTaskbar);
			Assert.IsTrue(window.Topmost);
			Assert.AreEqual(ResizeMode.CanResizeWithGrip, window.ResizeMode);
			Assert.AreEqual(SizeToContent.Height, window.SizeToContent);
			Assert.AreEqual(WindowStartupLocation.CenterOwner, window.WindowStartupLocation);
			Assert.AreEqual(WindowState.Maximized, window.WindowState);
			Assert.AreEqual(WindowStyle.None, window.WindowStyle);
			Assert.AreSame(style, window.Style);
		}

		[Test]
		public void ApplyTo_Window_ShouldNotApplyAnyPropertiesNotExplicitlySet()
		{
			Window window = new Window();
			Style style = new Style();
			style.TargetType = typeof(Window);
			style.Setters.Add(new Setter(Window.WindowStateProperty, WindowState.Maximized));
			_windowSmartPartInfo.WpfStyle = style;
			_windowSmartPartInfo.ApplyTo(window);

			//all these properties should just take their default value because they weren't explicitly set in the WindowSmartPartInfo
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.TitleProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.WidthProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.HeightProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.MinWidthProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.MinHeightProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.MaxWidthProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.MaxHeightProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.LeftProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.TopProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.ShowInTaskbarProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.TopmostProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.ResizeModeProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.SizeToContentProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.WindowStyleProperty).BaseValueSource);
			Assert.AreEqual(BaseValueSource.Default, DependencyPropertyHelper.GetValueSource(window, Window.IconProperty).BaseValueSource);

			//the style was explicitly provided via the WindowSmartPartInfo
			Assert.AreEqual(BaseValueSource.Local, DependencyPropertyHelper.GetValueSource(window, Window.StyleProperty).BaseValueSource);

			//this property should come from the style
			Assert.AreEqual(BaseValueSource.Style, DependencyPropertyHelper.GetValueSource(window, Window.WindowStateProperty).BaseValueSource);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ApplyTo_WindowSmartPartInfo_ShouldThrowIfWindowSmartPartInfoIsNull()
		{
			_windowSmartPartInfo.ApplyTo((WindowSmartPartInfo) null);
		}

		[Test]
		public void ApplyTo_WindowSmartPartInfo_ShouldApplyPropertiesToGivenWindowSmartPartInfo()
		{
			Style style = new Style();
			ImageSource icon = new DrawingImage();
			WindowSmartPartInfo destination = new WindowSmartPartInfo();

			_windowSmartPartInfo.IsModal = true;
			_windowSmartPartInfo.Width = 200;
			_windowSmartPartInfo.Height = 300;
			_windowSmartPartInfo.MinWidth = 50;
			_windowSmartPartInfo.MinHeight = 40;
			_windowSmartPartInfo.MaxWidth = 400;
			_windowSmartPartInfo.MaxHeight = 350;
			_windowSmartPartInfo.Left = 45;
			_windowSmartPartInfo.Top = 55;
			_windowSmartPartInfo.ShowInTaskbar = false;
			_windowSmartPartInfo.IsTopmost = true;
			_windowSmartPartInfo.ResizeMode = ResizeMode.CanResizeWithGrip;
			_windowSmartPartInfo.SizeToContent = SizeToContent.Height;
			_windowSmartPartInfo.StartupLocation = WindowStartupLocation.CenterOwner;
			_windowSmartPartInfo.State = WindowState.Maximized;
			_windowSmartPartInfo.Style = WindowStyle.None;
			_windowSmartPartInfo.WpfStyle = style;
			_windowSmartPartInfo.Icon = icon;
			_windowSmartPartInfo.DialogResult = true;

			_windowSmartPartInfo.ApplyTo(destination);

			Assert.AreEqual(200, destination.Width);
			Assert.AreEqual(300, destination.Height);
			Assert.AreEqual(50D, destination.MinWidth);
			Assert.AreEqual(40D, destination.MinHeight);
			Assert.AreEqual(400D, destination.MaxWidth);
			Assert.AreEqual(350D, destination.MaxHeight);
			Assert.AreEqual(45D, destination.Left);
			Assert.AreEqual(55D, destination.Top);
			Assert.IsFalse(destination.ShowInTaskbar.Value);
			Assert.IsTrue(destination.IsTopmost.Value);
			Assert.AreEqual(ResizeMode.CanResizeWithGrip, destination.ResizeMode);
			Assert.AreEqual(SizeToContent.Height, destination.SizeToContent);
			Assert.AreEqual(WindowStartupLocation.CenterOwner, destination.StartupLocation);
			Assert.AreEqual(WindowState.Maximized, destination.State);
			Assert.AreEqual(WindowStyle.None, destination.Style);
			Assert.AreSame(style, destination.WpfStyle);
			Assert.IsTrue(GetPrivateMemberValue<bool>(destination, "_wpfStyleSet"));
			Assert.AreSame(icon, destination.Icon);
			Assert.IsTrue(GetPrivateMemberValue<bool>(destination, "_iconSet"));
			Assert.IsTrue(_windowSmartPartInfo.DialogResult.Value);
		}
	}
}
